package com.sxkiler.demo.junitcase;

import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.connection.ClusterSettings;
import com.sxkiler.demo.elasticsearch.entity.RoomVo;
import com.sxkiler.demo.vo.SlowInterfaceVo;
import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;

import java.util.ArrayList;
import java.util.List;

public class MongodbClientTestCase {

    static String host = "172.20.176.216";
    static int port = 27017;
    static String username = "kingdee";
    static String database = "ownercloud_alpha2";
    static String password = "wojiayun2017";
    static String authentication_database = "ownercloud_alpha";

    @Test
    public void testConnect(){
        //构建mongodClient配置
        MongoClientSettings.Builder builder = MongoClientSettings.builder();
        //构建集群配置
        List<ServerAddress> addresses = new ArrayList<ServerAddress>();
        ServerAddress address = new ServerAddress(host,port);
        addresses.add(address);
        builder.applyToClusterSettings(builder1 -> {
            builder1.hosts(addresses);
        });
        //加密配置
        builder.credential(MongoCredential.createCredential(username, authentication_database, password.toCharArray()));
        //创建client
        MongoClient client = MongoClients.create(builder.build());
        SimpleMongoClientDbFactory dbFactory =  new SimpleMongoClientDbFactory(client,database);
        MongoTemplate mongoTemplate = new MongoTemplate(dbFactory);
        List<SlowInterfaceVo> list = mongoTemplate.findAll(SlowInterfaceVo.class);
        System.out.println(list.size());
    }
}
